<template>
  <div class="statistics-container">
    <div class="statistics-header">
      <h2>数据统计</h2>
    </div>
    
    <div class="statistics-cards">
      <div class="stat-card" v-for="(item, index) in statisticsData" :key="index">
        <div class="card-icon">
          <img :src="item.icon" :alt="item.title" />
        </div>
        <div class="card-content">
          <div class="card-number">{{ item.value }}</div>
          <div class="card-title">{{ item.title }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getStatistics } from './api/statistics'
export default {
  name: 'Statistics',
  data() {
    return {
      statisticsData: [
        {
          icon: require('@/assets/s-5.png'),
          value: 0,
          title: '回购/KG',
          key: 'sellCount'
        },
        {
          icon: require('@/assets/s-4.png'),
          value: 0,
          title: '销售/KG',
          key: 'buyCount'
        },
        {
          icon: require('@/assets/s-5.png'),
          value: 0,
          title: '回购均价（元/g）',
          key: 'sellAvgPrice'
        },
        {
          icon: require('@/assets/s-4.png'),
          value: 0,
          title: '销售均价（元/g）',
          key: 'buyAvgPrice'
        },
        {
          icon: require('@/assets/s-3.png'),
          value: 0,
          title: '净/KG',
          key: 'netCount'
        },
        {
          icon: require('@/assets/s-6.png'),
          value: 0,
          title: '净手数',
          key: 'netAsCount'
        },
        {
          icon: require('@/assets/s-5.png'),
          value: 0,
          title: '回购/水贝',
          key: 'goldBuyPrice'
        },
        {
          icon: require('@/assets/s-4.png'),
          value: 0,
          title: '销售/水贝',
          key: 'goldSellPrice'
        },
        {
          icon: require('@/assets/s-5.png'),
          value: 0,
          title: '回购/外盘',
          key: 'goldUsdBuyPrice'
        },
        {
          icon: require('@/assets/s-4.png'),
          value: 0,
          title: '销售/外盘',
          key: 'goldUsdSellPrice'
        },
        {
          icon: require('@/assets/s-5.png'),
          value: 0,
          title: '回购/基差',
          key: 'buyBasis'
        },
        {
          icon: require('@/assets/s-4.png'),
          value: 0,
          title: '销售/基差',
          key: 'sellBasis'
        }
      ]
    }
  },
  mounted() {
    this.fetchStatisticsData()
    // 监听WebSocket消息事件
    this.$onWebSocket('statisticsUpdate', this.onMessageReceived);
  },
  methods: {
    async fetchStatisticsData() {
      try {
        const res = await getStatistics()
        this.updateStatisticsData(res)
      } catch (error) {
        console.error('获取统计数据失败:', error)
      }
    },
    // 格式化数字为千分符
    formatNumber(num) {
      if (typeof num !== 'number') return num
      return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')
    },
    updateStatisticsData(data) {
      this.statisticsData.forEach(item => {
        if (data[item.key] !== undefined) {
          item.value = this.formatNumber(data[item.key])
        }
      })
    },
    onMessageReceived(data) {
      this.updateStatisticsData(data.data)
    }
  },
}
</script>

<style scoped>
.statistics-container {
  padding: 0 12px;
  background-color: #f5f5f6;
  min-height: 100vh;
}

.statistics-header {
  margin-bottom: 24px;
}

.statistics-header h2 {
  color: #333;
  font-size: 24px;
  font-weight: 600;
  margin: 0;
}

.statistics-cards {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  width: 100%;
}

.stat-card {
  background: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  flex: 1 1 calc((100% - 32px) / 3);
  min-width: 0;
  display: flex;
  align-items: center;
  transition: all 0.3s ease;
}

.stat-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
}

.card-icon {
  margin-right: 20px;
  flex-shrink: 0;
}

.card-icon img {
  width: 48px;
  height: 48px;
  object-fit: contain;
}

.card-content {
  flex: 1;
}

.card-number {
  font-size: 32px;
  font-weight: 700;
  color: #1890ff;
  margin-bottom: 8px;
  line-height: 1;
}

.card-title {
  font-size: 16px;
  color: #666;
  font-weight: 500;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .statistics-cards {
    flex-direction: column;
  }
  
  .stat-card {
    min-width: auto;
  }
  
  .statistics-container {
    padding: 16px;
  }
}
</style>
